package com.leesche.zcsp.node.web.controller.wechat.register;

import com.leesche.zcsp.node.utils.security.QEncodeUtil;
import com.leesche.zcsp.node.utils.web.SessionManager;
import com.leesche.zcsp.node.utils.web.gernerator.TimeGreaterUilt;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.controller.template.UserTemplate;
import com.leesche.zcsp.node.web.model.PubCustomer;
import com.leesche.zcsp.node.web.model.SysUser;
import com.leesche.zcsp.node.web.vo.Principal;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author Jerry Chan
 * @version V1.0
 * @Title: node
 * @Package com.leesche.zcsp.node.web.controller.wechat.register
 * @Description:( 微信用户注册控制器 )
 * @date 2016年10月18日 17:21
 */
@Controller
@RequestMapping(value = "/wechat/register")
public class WachatRegisterController extends UserTemplate {

    private static final Logger logger = Logger.getLogger(WachatRegisterController.class);

    private static final String REGISTER_ACCESS_VIEW = "wechat/register/register";


    @RequestMapping(value = "/gotoRegister", method = RequestMethod.GET)
    public String showUserInfoView() {
        return REGISTER_ACCESS_VIEW;
    }


    @ResponseBody
    @RequestMapping(value = "{code}/submitUserInfo", method = RequestMethod.POST)
    public Map<String, Object> addWechatUserInfo(final SysUser sysUser, @PathVariable("code") String code) {
        try {
            return createUserInfo(sysUser, code, Constants.MODULE_TYPE_WECHAT);
        } catch (Exception e) {
            // TODO: handle exception
            logger.error(e, e);
            return WebResult.failure(e).toMap();
        }
    }

    @ResponseBody
    @RequestMapping(value = "verifyRegisterCode", method = RequestMethod.GET)
    public Map<String, Object> verifyCode(final @RequestParam String mobile, final @RequestParam String mobilecode) {
        try {

            if ((mobile != null || !"".equals(mobile)) && (mobilecode != null || !"".equals(mobilecode))) {
                String sessionCode = (String) SessionManager.getSessionAttribute(Constants.MOBILE_SMS_CODE);
                String sessionMobile = (String) SessionManager.getSessionAttribute(Constants.MOBILE_NUMBER);

                if ((sessionCode == null || "".equals(sessionCode)) || (sessionMobile == null || "".equals(sessionMobile))) {
                    return WebResult.failure("短信验证码已过期。").toMap();
                } else {

                    if (!mobile.equals(sessionMobile)) {
                        return WebResult.failure("请使用接收短信的手机号码。").toMap();
                    } else if (!mobilecode.equals(sessionCode)) {
                        return WebResult.failure("请输入正确的短信验证码。").toMap();
                    } else {
                        return WebResult.success().info("图片验证及短信验证码正确。").toMap();
                    }
                }
            } else {
                return WebResult.failure("短信验证码、接收短信手机号不能为空。").toMap();
            }

        } catch (Exception e) {
            logger.error(e, e);
            return WebResult.failure(e).toMap();
        }
    }

}
